#include <SFML/Graphics.hpp>
#include "GUIManager.h"

using namespace sf;
using namespace SwfUI;

bool running = true;
sf::Color background(0,255,0);

Argument exitClick(GUIControl*,const ArgumentList&)
{
	running = false;
	return Argument::Null;
}

Argument colorChange(GUIControl*,const ArgumentList& args)
{
	if(args.size() >= 1)
	{
		background = args[0];
	}
	return Argument::Null;
}

Argument opacityChange(GUIControl* caller,const ArgumentList& args)
{
	if(args.size() >= 1)
	{
		int alpha = (int)(02.55f *(float)args[0]);
		caller->SetColor(Color(255,255,255,alpha));
	}
	return Argument::Null;
}

int main()
{
	sf::RenderWindow window(sf::VideoMode(800,600),"Test");
	GUIManager guiManager(window.GetInput());
	guiManager.addFunction(L"exitClick",&exitClick);
	guiManager.addFunction(L"colorChange",&colorChange);
	guiManager.addFunction(L"opacityChange",&opacityChange);

	GUIControl* test = guiManager.createControl(512,512);
	test->load(L"controls.swf");
	test->SetPosition(100.0f,200.0f);
	test->SetRotation(45.0f);
	test->setRenderQuality(SwfUI::RQ_AUTOHIGH);
	test->setTransparent(true);
	test->setTitleBarHeight(40);

	GUIControl* fps = guiManager.createControl(128,128);
	fps->load(L"fps.swf");
	fps->setTransparent(true);
	fps->setRenderQuality(SwfUI::RQ_AUTOLOW);
	fps->setTitleBarHeight(128);

	SwfUI::ArgumentList args;

	while(window.IsOpened() && running)
	{
		sf::Event ev;
		while(window.GetEvent(ev))
		{
			if(ev.Type==sf::Event::Closed)
				window.Close();

			guiManager.processEvent(ev);
		}

		args.clear();
		if(window.GetFrameTime() > 0.0f)
			args(1.0f / window.GetFrameTime());
		else
			args(0);

		fps->callFunction(L"setFPS",args);

		window.Clear(background);
		guiManager.render(window);
		window.Display();
	}
}